1
/************************************* Module Header **************************************\
2 * Module Name: CSVSMonitorFileChangePackage.cs
3 * Project: CSVSPackageMonitorFileChange
4 * Copyright (c) Microsoft Corporation.
6 * Visual Studio provides SVsFileChangeEx service enables arbitrary components
7 * to register to be notified when a file is modified outside of the Environment.
9 * This service is useful when you are performing some operation which will be
10 * interupted by file changes from outside environment.
12 * The service is similar with FileSystemWatcher Class.
13 * http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
15 * To use this sample, follow the steps as below:
17 * 1. Start experimental VS instance
18 * 2. The package will be autoloaded automatically when there is no any solution
20 * 3. The demo monitors user's desktop directory, so please do file or directory
22 * 4. Visual Studio will popup window whenever a change is made.
24 * This source is subject to the Microsoft Public License.
25 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
26 * All other rights reserved.
29 * * 1/6/2010 5:25PM Hongye Sun Created
30 \******************************************************************************************/
33 using System
.Diagnostics
;
34 using System
.Globalization
;
35 using System
.Runtime
.InteropServices
;
36 using System
.ComponentModel
.Design
;
37 using Microsoft
.Win32
;
38 using Microsoft
.VisualStudio
.Shell
.Interop
;
39 using Microsoft
.VisualStudio
.OLE
.Interop
;
40 using Microsoft
.VisualStudio
.Shell
;
41 using Microsoft
.VisualStudio
;
42 using System
.Windows
.Forms
;
44 namespace CSVSPackageMonitorFileChange
47 /// This is the class that implements the package exposed by this assembly.
49 /// The minimum requirement for a class to be considered a valid package for Visual Studio
50 /// is to implement the IVsPackage interface and register itself with the shell.
51 /// This package uses the helper classes defined inside the Managed Package Framework (MPF)
52 /// to do it: it derives from the Package class that provides the implementation of the
53 /// IVsPackage interface and uses the registration attributes defined in the framework to
54 /// register itself and its components with the shell.
56 // This attribute tells the registration utility (regpkg.exe) that this class needs
57 // to be registered as package.
58 [PackageRegistration(UseManagedResourcesOnly
= true)]
59 // A Visual Studio component can be registered under different regitry roots; for instance
60 // when you debug your package you want to register it in the experimental hive. This
61 // attribute specifies the registry root to use if no one is provided to regpkg.exe with
63 [DefaultRegistryRoot("Software\\Microsoft\\VisualStudio\\9.0")]
64 // This attribute is used to register the informations needed to show the this package
65 // in the Help/About dialog of Visual Studio.
66 [InstalledProductRegistration(false, "#110", "#112", "1.0", IconResourceID
= 400)]
67 // In order be loaded inside Visual Studio in a machine that has not the VS SDK installed,
68 // package needs to have a valid load key (it can be requested at
69 // http://msdn.microsoft.com/vstudio/extend/). This attributes tells the shell that this
70 // package has a load key embedded in its resources.
71 [ProvideLoadKey("Standard", "1.0", "CSVSMonitorFileChange", "Microsoft", 1)]
72 [Guid(GuidList
.guidCSVSMonitorFileChangePkgString
)]
73 [ProvideAutoLoad("{adfc4e64-0397-11d1-9f4e-00a0c911004f}")]
74 public sealed class CSVSMonitorFileChangePackage
: Package
76 CSVSMonitorFileChange monitor
;
79 /// Default constructor of the package.
80 /// Inside this method you can place any initialization code that does not require
81 /// any Visual Studio service because at this point the package object is created but
82 /// not sited yet inside Visual Studio environment. The place to do all the other
83 /// initialization is the Initialize method.
85 public CSVSMonitorFileChangePackage()
87 Trace
.WriteLine(string.Format(CultureInfo
.CurrentCulture
, "Entering constructor for: {0}", this.ToString()));
92 /////////////////////////////////////////////////////////////////////////////
93 // Overriden Package Implementation
94 #region Package Members
97 /// Initialization of the package; this method is called right after the package is sited, so this is the place
98 /// where you can put all the initilaization code that rely on services provided by VisualStudio.
100 protected override void Initialize()
102 Trace
.WriteLine(string.Format(CultureInfo
.CurrentCulture
, "Entering Initialize() of: {0}", this.ToString()));
105 IVsFileChangeEx fileChangeService
=
106 GetService(typeof(SVsFileChangeEx
)) as IVsFileChangeEx
;
107 monitor
= new CSVSMonitorFileChange();
110 // Enables a client to receive notifications of changes to a directory.
111 fileChangeService
.AdviseDirChange(
113 // String form of the moniker identifier of
114 // the directory in the project system.
115 Environment
.GetFolderPath(
116 Environment
.SpecialFolder
.Desktop
),
118 // If true, then events should also be fired
119 // for changes to sub directories. If false,
120 // then events should not be fired for changes
121 // to sub directories.
122 Convert
.ToInt32(true),
124 // IVsFileChangeEvents Interface on the object
125 // requesting notification of file change events.
128 // Unique identifier for the file that is
129 // associated with the event sink.